package com.nodecollege.cloud.controller.admin;

import com.nodecollege.cloud.common.annotation.AdminLoginAnnotation;
import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.exception.UpmsException;
import com.nodecollege.cloud.common.model.BindVO;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.UpmsMenu;
import com.nodecollege.cloud.common.model.po.UpmsTenant;
import com.nodecollege.cloud.common.model.po.UpmsTenantApp;
import com.nodecollege.cloud.common.utils.NCUtils;
import com.nodecollege.cloud.service.MenuService;
import com.nodecollege.cloud.service.TenantAppService;
import com.nodecollege.cloud.service.TenantMenuService;
import com.nodecollege.cloud.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 租户管理
 *
 * @author LC
 * @date 2019/6/19 17:38
 */
@RestController
@AdminLoginAnnotation
@RequestMapping("/admin/tenant")
public class TenantController {

    @Autowired
    private TenantService tenantService;

    @Autowired
    private TenantAppService tenantAppService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private TenantMenuService tenantMenuService;

    @ApiAnnotation(modularName = "租户管理", description = "获取所有租户信息")
    @PostMapping("/getTenantList")
    public NCResult<UpmsTenant> getTenantList(@RequestBody QueryVO<UpmsTenant> queryVO) {
        return tenantService.getTenantList(queryVO);
    }

    @ApiAnnotation(modularName = "租户管理", description = "更新租户信息")
    @PostMapping("/updateTenant")
    public NCResult updateTenant(@RequestBody UpmsTenant upmsTenant) {
        tenantService.updateTenant(upmsTenant);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "租户应用管理", description = "查询租户应用列表缓存")
    @PostMapping("/getTenantAppListCache")
    public NCResult<UpmsTenantApp> getTenantAppListCache(@RequestBody UpmsTenantApp tenantApp) {
        return NCResult.ok(tenantAppService.getTenantAppListCache(tenantApp.getTenantId()));
    }

    @ApiAnnotation(modularName = "租户应用管理", description = "清除租户应用列表缓存")
    @PostMapping("/clearTenantAppListCache")
    public NCResult clearTenantAppListCache(@RequestBody UpmsTenantApp tenantApp) {
        tenantAppService.clearTenantAppListCache(tenantApp.getTenantId());
        return NCResult.ok("清除租户应用列表缓存成功！");
    }

    @ApiAnnotation(modularName = "租户应用管理", description = "绑定租户应用")
    @PostMapping("/bindTenantApp")
    public NCResult bindTenantApp(@RequestBody BindVO bindVO) {
        tenantAppService.bindTenantApp(bindVO);
        return NCResult.ok("绑定租户应用成功！");
    }

    @ApiAnnotation(modularName = "租户应用管理", description = "锁定解锁租户应用")
    @PostMapping("/lockTenantApp")
    public NCResult lockTenantApp(@RequestBody BindVO bindVO) {
        tenantAppService.updateTenantAppState(bindVO);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "租户应用管理", description = "查询租户应用菜单列表")
    @PostMapping("/getMenuListByTenantId")
    public NCResult<UpmsMenu> getMenuListByTenantId(@RequestBody UpmsMenu menu) {
        NCUtils.nullOrEmptyThrow(menu.getTenantId(), new UpmsException("-1", "租户id必填！"));
        menu.setMenuSource(NCConstants.INT_0);
        return NCResult.ok(menuService.getMenuList(menu));
    }

    @ApiAnnotation(modularName = "租户应用资源管理", description = "绑定租户应用资源")
    @PostMapping("/bindTenantMenu")
    public NCResult bindTenantMenu(@RequestBody BindVO bindVO) {
        NCUtils.nullOrEmptyThrow(bindVO.getAppId(), new UpmsException("-1", "应用id必填！"));
        bindVO.setMainSource(true);
        tenantMenuService.bindTenantMenu(bindVO);
        return NCResult.ok("绑定租户菜单成功！");
    }

    @ApiAnnotation(modularName = "租户应用资源管理", description = "锁定/解锁租户菜单")
    @PostMapping("/lockTenantMenu")
    public NCResult lockTenantMenu(@RequestBody BindVO bindVO) {
        tenantMenuService.updateTenantMenuState(bindVO);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "租户管理", description = "根据租户id查询租户信息", accessSource = 2)
    @PostMapping("/getTenantById")
    public NCResult<UpmsTenant> getTenantById(@RequestBody UpmsTenant upmsTenant) {
        UpmsTenant tenant = tenantService.getTenantById(upmsTenant);
        return NCResult.ok(tenant);
    }
}
